package models;

import models.constants.DeletedStatus;
import product.ProductSpec;
import util.common.ConvertUtil;
import util.common.UStringUtil;

import javax.persistence.*;
import java.util.List;

/**
 * Created by liming on 16/7/16.
 */
@Entity
@Table(name = "admin_stock_init_item")
public class AdminStockInitItem extends BaseModel {
    /**
     * 库存初始化
     */
    @ManyToOne
    @JoinColumn(name = "admin_stock_init_id")
    public AdminStockInit adminStockInit;

    /**
     * 产品规格,注意这里选择产品只能不能选择套餐,注意展现出产品分类字段
     */
    @ManyToOne
    @JoinColumn(name = "product_spec_id")
    public ProductSpec productSpec;

    /**
     * 数量
     */
    @Column(name = "num")
    public Double num = 0D;

    /**
     * 成本单价
     */
    @Column(name = "price")
    public Double price = 0D;

    /**
     * 金额
     */
    @Column(name = "amount")
    public Double amount = 0D;

    /**
     * 备注
     */
    @Column(name = "remark")
    public String remark;

    /**
     * 根据初始化库存表查询子表(AdminStockInitItem)集合
     */
    public static List<AdminStockInitItem> findAdminStockInitItem(long stockInitId) {
        return AdminStockInitItem.find("adminStockInit.id=? and adminStockInit.deletedStatus=?", stockInitId, DeletedStatus.UN_DELETED).fetch();
    }

    /**
     * 验证同一仓库的同一种产品库存是否已存在
     */

    public static String auditIsInit(List<Integer> proSpecIdList) {
        String proSpecStr = "目前";
        if (proSpecIdList != null ) {
            String proSpecIdId = UStringUtil.concatStr(",", proSpecIdList);
            List<AdminStockInitItem> adminStockInitItemList = AdminStockInitItem.find(" adminStockInit.deletedStatus=? and productSpec.id in (" + proSpecIdId + ")", DeletedStatus.UN_DELETED).fetch();
            if (adminStockInitItemList != null) {
                for (AdminStockInitItem stockPro : adminStockInitItemList) {
                    proSpecStr += stockPro.productSpec.product.name + stockPro.productSpec.spec + ",";
                }
            }
        }
        return proSpecStr;
    }

    /**
     * 根据ID查询被初始化的产品
     */
    public static AdminStockInitItem findByItemId(long id) {
        return AdminStockInitItem.find("id=? and adminStockInit.deletedStatus=?", id, DeletedStatus.UN_DELETED).first();
    }

    /**
     * 根据id删除adminStockInitItem
     * param delItemIdArray
     */
    public static void delByIdArrays(Integer[] delItemIdArray) {
        String delItemId = UStringUtil.concatStr(",", delItemIdArray);
        AdminStockInitItem.delete("id in(" + delItemId + ")");
    }

    /**
     * 验证库存初始化是否存在已经有库存的产品
     */
    public final static String SQL_VALIDATE_STOCK_NUM =
            "select GROUP_CONCAT(d.name,'(',c.spec,')')" +
            " from admin_stock_init_item a" +
            " left join admin_product_spec_stock b on a.product_spec_id = b.product_spec_id" +
            " left join product_specs c on b.product_spec_id = c.id" +
            " left join products d on c.product_id = d.id" +
            " where a.admin_stock_init_id = ? and  b.stock_num!=0" ;
    public static String validateStockInitExist(long stockInitId){
        String resultStr = ConvertUtil.toString(em().createNativeQuery(SQL_VALIDATE_STOCK_NUM).setParameter(1,stockInitId).getSingleResult());
        if(resultStr.length()>0)
            resultStr = resultStr.replace(",","<br>");
        return resultStr;
    }

}
